﻿using DotRas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComPortClass
{
    public class DialConnection
    {
        public DialConnection(String ConnectionName, string DialNumber)
        {
            this.ConnectionName = ConnectionName;
            this.DialNumber = DialNumber;
        }
        public string ConnectionName { get; set; }

        public string DialNumber { get; set; }

        public List<String> GetAllUserPhoneBook()
        {
            DotRas.RasPhoneBook allUsersPhoneBook = new DotRas.RasPhoneBook();
            allUsersPhoneBook.Open(true);
            List<String> res = new List<String>();
            if (allUsersPhoneBook.Entries.Count > 0)
            {
                allUsersPhoneBook.Entries.ToList().ForEach(p => res.Add(p.Name));
            }
            return res;
        }
        public CreateConnectionStatus CreateConnection( RasDevice Device)
        {
            DotRas.RasDialer dialer = new DotRas.RasDialer();
            DotRas.RasPhoneBook allUsersPhoneBook = new DotRas.RasPhoneBook();
            allUsersPhoneBook.Open(true);
            if (allUsersPhoneBook.Entries.Contains(ConnectionName))
            {
                return CreateConnectionStatus.Exsit;
            }
            try
            {
                allUsersPhoneBook.Entries.Add(RasEntry.CreateDialUpEntry(ConnectionName, DialNumber, Device));
                return CreateConnectionStatus.Success;
            }
            catch(Exception ex)
            {
                throw ex;
                //return CreateConnectionStatus.Error;
            }
        }

        public CreateConnectionStatus CreateConnection()
        {
            return CreateConnection(RasDevice.GetDevices().First(p => p.DeviceType == RasDeviceType.PPPoE));
        }
    }

    public enum CreateConnectionStatus
    {
        Success,Exsit,Error
    }
}
